import We_layerEvent from '@/we-api/we-layer/layer/event'
import { MyLayerEnum } from '../my-layer-enum'
import {
  viewModel_GridColorAndValueRamp,
  viewModel_IsoSurfaceColorAndValueRamp,
  viewModel_VolumeColorAndValueRamp,
  viewModel_WindColorAndValueRamp,
} from '@/view-model/legend'
import { We_LayerEnum } from '@/we-api/we-layer/enum-legend'

export const setupLayerEvent = (WE) => {
  We_layerEvent.onAdd = (layerName: string) => {
    const layer: WeatherEarth.WeImageryLayer = WE.layerManager.getById(layerName)
    if (!layer) return
    if (layerName === MyLayerEnum.Wind) {
      viewModel_WindColorAndValueRamp.value = layer.display.valueAndColorRamp
    } else if (layerName === We_LayerEnum.FeatureGrid) {
      viewModel_GridColorAndValueRamp.value = layer.display.valueAndColorRamp
    } else if (layerName === We_LayerEnum.FeatureVolume) {
      viewModel_VolumeColorAndValueRamp.value = layer.display.valueAndColorRamp
    } else if (layerName === We_LayerEnum.FeatureIsoSurface) {
      viewModel_IsoSurfaceColorAndValueRamp.value = layer.display.valueAndColorRamp
    } else if (
      layerName === We_LayerEnum.SceneObject1 ||
      layerName === We_LayerEnum.SceneObject2 ||
      layerName === We_LayerEnum.SceneObject3
    ) {
      if (layer.display.valueDecoder.isUV) {
        viewModel_WindColorAndValueRamp.value = layer.display.valueAndColorRamp
      }
    }
  }

  We_layerEvent.onRemove = (layerName: string) => {
    if (layerName === MyLayerEnum.Wind) {
      viewModel_WindColorAndValueRamp.value = null
    } else if (
      layerName === We_LayerEnum.SceneObject1 ||
      layerName === We_LayerEnum.SceneObject2 ||
      layerName === We_LayerEnum.SceneObject3
    ) {
      const layer: WeatherEarth.WeImageryLayer = WE.layerManager.getById(layerName)
      if (layer && layer.display && layer.display.valueDecoder && layer.display.valueDecoder.isUV) {
        viewModel_WindColorAndValueRamp.value = null
      }
    } else if (layerName === We_LayerEnum.FeatureGrid) {
      viewModel_GridColorAndValueRamp.value = null
    } else if (layerName === We_LayerEnum.FeatureVolume) {
      viewModel_VolumeColorAndValueRamp.value = null
    } else if (layerName === We_LayerEnum.FeatureIsoSurface) {
      viewModel_IsoSurfaceColorAndValueRamp.value = null
    }

    const layer = WE.layerManager.getById(layerName)
    if (layer && layer.dataProvider && layer.dataProvider.timeSampleArray) {
      // const WE = We_getInstance()
      // WE.timeSystem.zoomToToday()
    }
  }
}
